'''
    capture_extensible.py
    捕获报文程序，此处正在慢慢优化补充
'''

from data_type_extensible import *
from module_of_msg import *

def Matching_message(string: list, mode: list, dep = 0, automode = 1, father: msg_struct = None):  
    '''
    根据给定报文分析模式进行分析报文
    string: 报文的二进制文件，格式为list，每个元素代表1byte，方便小端
    mode: 元素为msg_mode的list
    '''
    global BigOrSmall
    if (len(mode) != 0):
        # print(type(mode[0]))
        print(mode[0].mode_name)
    # print('dep: ', dep)
    dep += 1
    if len(mode) == 0 or mode[0].mode_id == 0x0:
        strlen = len(string)
        # print(type(string))
        temp = msg_segment('data', strlen, string)
        msg = msg_struct(0, 0x0)
        msg.add_seg(temp)
        # print(msg)
        return (msg, [])
    now_mode = mode[0]
    now_msg = msg_struct(0, now_mode.mode_id)
    now_msg.set_father(father)
    mode = mode[1:]
    pointer = 0
    # remain_seg = len(now_mode.mode)
    for segtype, segname, seglen in now_mode.mode:
        # print(now_mode.mode_id, segname, seglen)
        if segtype == 0:
            if segname == 'data':
                if now_mode.datalen_name == None:
                    next_mode = 0
                    if automode:
                        next_mode = now_msg.get_next_mode(now_mode)
                        next_mode = rev_exc_dict(next_mode)
                        next_mode = [next_mode]
                    (ret, tmp) = Matching_message(string[pointer: ], next_mode, dep, automode, now_msg)
                # print('data:',)
                else:
                    length_from_data = now_mode.datalen * 8
                    next_mode = 0
                    if automode:
                        next_mode = now_msg.get_next_mode(now_mode)
                        next_mode = rev_exc_dict(next_mode)
                        next_mode = [next_mode]
                    (ret, tmp) = Matching_message(string[pointer: pointer + length_from_data], next_mode, dep, automode, now_msg)
                # print(ret.msg_seg[0].seg_data)
                temp = msg_segment(segname, ret.msg_len, ret)
                # print(temp.seg_data)
                pointer += ret.msg_len
                # now_msg.msg_len += ret.msg_len
            else:
                # print(segname)
                data = string[pointer: pointer + seglen]
                print("seg:", segname, data)
                len_data = len(data)
                ret = 0
                if (BigOrSmall == 0 and now_mode.mode_datamode == -1) or now_mode.mode_datamode == 0:
                    for i in range(len_data):
                        ret = ret * 0x2 + data[i]
                else:
                    for i in range(len_data // 8):
                        temp = 0
                        for j in range(8):
                            temp = temp * 0x2 + data[i * 8 + j]
                        ret = ret + temp * (0x100)**i
                temp = msg_segment(segname, seglen, ret)
                pointer += seglen
                # now_msg.msg_len += seglen
                print(temp.seg_name, temp.seg_data)
                if segname == now_mode.datalen_name:
                    now_mode.datalen = ret
                    # print('datalen', ret)
                if segname == 'Magic' and now_mode.mode_id == 0xFFFFFFFF:
                    if ret == 0xd4c3b2a1:
                        BigOrSmall = 1
                    else:
                        BigOrSmall = 0
            # print("now", now_msg.msg_len)
            now_msg.add_seg(temp)
        else:
            if segname == 'data':
                now_len = seglen(now_msg)
                length_from_data = now_len * 8
                next_mode = 0
                if automode:
                    next_mode = now_msg.get_next_mode(now_mode)
                    next_mode = rev_exc_dict(next_mode)
                    next_mode = [next_mode]
                (ret, tmp) = Matching_message(string[pointer: pointer + length_from_data], next_mode, dep, automode, now_msg)
                # print(ret.msg_seg[0].seg_data)
                temp = msg_segment(segname, ret.msg_len, ret)
                # print(temp.seg_data)
                pointer += ret.msg_len
                # now_msg.msg_len += ret.msg_len
                now_msg.add_seg(temp)
            else:
                now_len = seglen(now_msg)
                print(now_len)
                data = string[pointer: pointer + now_len]
                print("seg:", segname, data)
                len_data = len(data)
                ret = 0
                if (BigOrSmall == 0 and now_mode.mode_datamode == -1) or now_mode.mode_datamode == 0:
                    for i in range(len_data):
                        ret = ret * 0x2 + data[i]
                else:
                    for i in range(len_data // 8):
                        temp = 0
                        for j in range(8):
                            temp = temp * 0x2 + data[i * 8 + j]
                        ret = ret + temp * (0x100)**i
                temp = msg_segment(segname, now_len, ret)
                pointer += now_len
                # now_msg.msg_len += now_len
                if segname == now_mode.datalen_name:
                    now_mode.datalen = ret
                now_msg.add_seg(temp)
            
    # print(now_msg)
    return (now_msg, string[pointer: ])
